package com.atguigu.gmall.comment.api;

import com.atguigu.gmall.comment.service.CommentInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.comment.CommentInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author atguigu-mqx
 */
@RestController
@RequestMapping("/api/comment/commentInfo")
public class CommentInfoApiController {
    @Autowired
    private CommentInfoService commentInfoService;

    @ApiOperation(value = "获取分页列表")
    @GetMapping("{spuId}/{page}/{limit}")
    public Result index(
            @ApiParam(name = "spuId", value = "spuId", required = true)
            @PathVariable Long spuId,

            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Integer page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Integer limit) {

        //  调用服务层的查询列表！
        IPage<CommentInfo> pageModel = commentInfoService.selectPage(page, limit, spuId);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "统计评论")
    @GetMapping("countBySpuId/{spuId}")
    public Result countBySpuId(@PathVariable Long spuId) {
        return Result.ok(commentInfoService.countBySpuId(spuId));
    }

    //  保存控制器
    @ApiOperation(value = "新增评论")
    @PostMapping("auth/save")
    public Result save(@RequestBody List<CommentInfo> commentInfoList, HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        commentInfoService.saveCommentInfo(commentInfoList, Long.parseLong(userId));
        return Result.ok();
    }

}
